package com.yunfei.yunmusic.web.control;

import com.yunfei.yunmusic.web.model.Comment;
import com.yunfei.yunmusic.web.model.SongInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SongInfoControl {
    @Autowired
    private  CommentPostControl commentPostControl;

    private static Map<String, SongInfo> songInfoMap = new HashMap<>();


    @RequestMapping(path = "/songinfo")
    public String index(String songId, ModelMap modelMap) {

        if (songId == null) {
            songId = "1346104327";
        }
        SongInfo songInfo = songInfoMap.get(songId);
        if(songInfo==null){
            return "errorPageDisplay";
        }else{
            modelMap.addAttribute("song", songInfo);
            List<Comment> comments=commentPostControl.getComments(songId);
            modelMap.addAttribute("comments",comments);
            return "index";
        }
    }

    @RequestMapping(value = "/songinfo/get")
    @ResponseBody     //返回一个json api
    public SongInfo get(String songId) {
        SongInfo songInfo = songInfoMap.get(songId);
        return songInfo;
    }

    @PostConstruct
    public void init() {
        SongInfo songInfo = new SongInfo();
        songInfo.setId("1346104327");
        songInfo.setName("多想在平庸的生活拥抱你");
        songInfo.setSinger("隔壁老樊");
        songInfo.setAlbumName("我曾");
        songInfo.setAlbumImg("http://p1.music.126.net/gNbAlXamNjhR2j3aOukNhg==/109951164232796511.jpg?param=177y177");
        songInfo.setCommentCount(9999);
        songInfo.setLyrics("世界上有很多的东西 <br/> 你生不带来死不带去 <br/> 你能带走的只有自己和自己的脾气<br/> ");
        songInfoMap.put("1346104327", songInfo);

        songInfo = new SongInfo();
        songInfo.setId("329925");
        songInfo.setName("最爱");
        songInfo.setSinger("周慧敏");
        songInfo.setAlbumName("最爱");
        songInfo.setAlbumImg("http://p1.music.126.net/Isy1oTrKeR3tsMi2qrNxBg==/26388279081798.jpg?param=130y130");
        songInfo.setCommentCount(999);
        songInfo.setLyrics("天空一片蔚蓝 <br/> 清风添上了浪漫 <br/> 心里那份柔情蜜意似海无线<br/> ");
        songInfoMap.put("329925", songInfo);


    }

}
